//
//  GWModeViewController.m
//  Glowalker
//
//  Created by 王华磊 on 2017/10/3.
//  Copyright © 2017年 Haley. All rights reserved.
//

#import "GWModeViewController.h"
#import "GWButton.h"

#import "BluetoothManager.h"
#import "NSString+QCExtension.h"

@interface GWModeViewController ()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (weak, nonatomic) IBOutlet UISlider *slider;

@property (nonatomic, strong) NSMutableArray  *topButtons;
@property (nonatomic, strong) UIView  *bottomLine;

@property (nonatomic, strong) NSMutableArray  *btns;

@end

@implementation GWModeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = showText(@"模式");
    
    [self initTopView];
    
    [self initSrollView];
    
    [self initBottomView];
}

- (void)setupLanguage
{
    self.navigationItem.title = showText(@"模式");
    [self.topButtons removeAllObjects];
    [self.topView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    self.bottomLine = nil;
    
    [self.btns removeAllObjects];
    [self.scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    [self initTopView];
    
    [self initSrollView];
}

- (void)initTopView
{
    self.topView.frame = CGRectMake(0, 0, kWidth, 44);
    
    UIImageView *topBgView = [[UIImageView alloc] initWithFrame:_topView.bounds];
    topBgView.image = [UIImage imageNamed:@"mote_tab_bg"];
    topBgView.contentMode = UIViewContentModeScaleToFill;
    [self.topView addSubview:topBgView];
    
    NSArray *topBtnTitles = @[showText(@"渐变"),showText(@"闪烁"),showText(@"酷炫")];
    self.topButtons = [[NSMutableArray alloc] initWithCapacity:topBtnTitles.count];
    
    CGFloat btnW = kWidth/topBtnTitles.count;
    CGFloat btnH = 44;
    for (int i = 0; i < topBtnTitles.count; i++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(btnW*i, 0, btnW, btnH)];
        button.tag = i;
        if (i == 0) {
            button.selected = YES;
        }
        [button setTitle:topBtnTitles[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor yellowColor] forState:UIControlStateSelected];
        button.titleLabel.font = [UIFont systemFontOfSize:15.0f];
        [button addTarget:self action:@selector(modeClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.topView addSubview:button];
        [self.topButtons addObject:button];
    }
    
    self.bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, 42, btnW, 2)];
    self.bottomLine.backgroundColor = [UIColor yellowColor];
    [self.topView addSubview:self.bottomLine];
}

- (void)initSrollView
{
    CGFloat srollViewH = kHeight-64-49-44-30;
    self.scrollView.frame = CGRectMake(0, 44, kWidth, srollViewH);
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(kWidth*3, srollViewH);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.backgroundColor = [UIColor clearColor];
    self.scrollView.delegate = self;
    
    NSArray *sceneNames = @[@"红色渐变",@"绿色渐变",@"蓝色渐变",
                            @"黄色渐变",@"青色渐变",@"紫色渐变",
                            @"白色渐变",@"红绿渐变",@"红蓝渐变",
                            @"绿蓝渐变",@"七彩渐变"];
    
    NSArray *flashNames = @[@"七彩频闪",@"红色频闪",@"绿色频闪",
                            @"蓝色频闪",@"黄色频闪",@"青色频闪",
                            @"紫色频闪",@"白色频闪"];
    
    NSArray *coolNames = @[@"模式1",@"模式2",@"模式3",
                          @"模式4",@"模式5",@"模式6",
                          @"模式7",@"模式8",@"模式9",
                          @"模式10"];
    
    NSArray *sceneCmds = @[@"070402",@"070401",@"070400",
                           @"070405",@"070403",@"070404",
                           @"070406",@"070430",@"070440",
                           @"070450",@"070420"];
    
    NSArray *flashCmds = @[@"070510",@"070502",@"070501",
                           @"070500",@"070505",@"070503",
                           @"070504",@"070506"];
    
    NSArray *coolCmds = @[@"07060100",@"070108",@"070113",
                          @"07030100",@"070900",@"070901",
                          @"070a01",@"070a02",@"070101",
                          @"070200"];
    NSArray *cmds = @[sceneCmds,flashCmds,coolCmds];
    NSArray *names = @[sceneNames,flashNames,coolNames];
    NSArray *prefixs = @[@"scene",@"flash",@"cool"];
    
    CGFloat btnW = kWidth/3;
    CGFloat btnH = btnW;
    self.btns = [[NSMutableArray alloc] init];
    for (int i = 0; i < 3; i++) {
        UIScrollView *srollView = [[UIScrollView alloc] initWithFrame:CGRectMake(kWidth*i, 0, kWidth, srollViewH)];
        srollView.tag = i;
        srollView.backgroundColor = [UIColor clearColor];
        
        NSArray *pageNames = names[i];
        NSUInteger rowNum = (pageNames.count + 2) / 3;
        CGFloat contentH = rowNum * btnH;
        srollView.contentSize = CGSizeMake(kWidth, contentH);
        
        NSString *prefix = prefixs[i];
        for (int j = 0; j < pageNames.count; j++) {
            int row = j / 3;
            int col = j % 3;
            // 创建一个按钮
            GWButton *button = [[GWButton alloc] initWithFrame:CGRectMake(btnW * col, btnH * row, btnW, btnH)];
            
            button.tag = j;
            button.cmd = cmds[i][j];
            button.textLabel.text = showText(pageNames[j]);
            
            NSString *imagePrefix = [NSString stringWithFormat:@"ic_%@_%d", prefix, j+1];
            NSString *imageName = [NSString stringWithFormat:@"%@_1", imagePrefix];
            button.iconImageView.image = [UIImage imageNamed:imageName];
            button.imagePrefix = imagePrefix;
            
            [srollView addSubview:button];
            
            button.iconImageView.animationDuration = 1;
            [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
            [self.btns addObject:button];
        }
        
        [self.scrollView addSubview:srollView];
    }
    
}

- (void)initBottomView
{
    self.bottomView.frame = CGRectMake(0, kHeight-64-49-30, kWidth, 30);
    self.bottomView.backgroundColor = [UIColor clearColor];
    self.slider.frame = CGRectMake(8, 0, kWidth - 16, 30);
    self.slider.minimumValue = 15;
    self.slider.maximumValue = 255;
    self.slider.minimumValueImage = [UIImage imageNamed:@"speed_decrease"];
    self.slider.maximumValueImage = [UIImage imageNamed:@"speed_increase"];
    [self.slider addTarget:self action:@selector(sliderChange:) forControlEvents:UIControlEventValueChanged];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (scrollView != self.scrollView) {
        return;
    }
    NSInteger index = (int)(scrollView.contentOffset.x / kWidth);
    if ((index + 1) > self.topButtons.count) {
        return;
    }
    
    for (UIButton *btn in self.topButtons) {
        btn.selected = NO;
    }
    UIButton *btn = self.topButtons[index];
    CGFloat btnW = kWidth/self.topButtons.count;
    btn.selected = YES;
    self.bottomLine.frame = CGRectMake(btnW*index, 42, btnW, 2);
}


#pragma mark - btn click events
- (void)modeClick:(UIButton *)btn
{
    NSInteger index = btn.tag;
    for (UIButton *btn in self.topButtons) {
        btn.selected = NO;
    }
   
    CGFloat btnW = kWidth/self.topButtons.count;
    btn.selected = YES;
    self.bottomLine.frame = CGRectMake(btnW*index, 42, btnW, 2);
    
    [self.scrollView setContentOffset:CGPointMake(index*kWidth, 0) animated:YES];
}

- (void)sliderChange:(UISlider *)slider
{
    int value = (int)slider.value;
    int factValue = 270 - value;
    NSLog(@"factValue---%d",factValue);
    NSString *cmd = [NSString stringWithFormat:@"10%@",[NSString ToHex:factValue]];
    [[BluetoothManager standardManager] writCommad:cmd];
}

- (void)btnClick:(GWButton *)btn
{
    for (GWButton *subBtn in self.btns) {
        [subBtn.iconImageView stopAnimating];
        subBtn.textLabel.textColor = [UIColor whiteColor];
    }
    
    NSString *imagePrefix = btn.imagePrefix;
    
    // 初始化按钮的动画图片
    NSMutableArray *animateImages = [NSMutableArray array];
    for (int k = 1; k < 5; k++) {
        NSString *imageName = [NSString stringWithFormat:@"%@_%d", imagePrefix, k];
        UIImage *image = [UIImage imageNamed:imageName];
        [animateImages addObject:image];
    }
    btn.iconImageView.animationImages = animateImages;
    
    [btn.iconImageView startAnimating];
    btn.textLabel.textColor = [UIColor redColor];
    NSString *cmd = btn.cmd;
    
    [[BluetoothManager standardManager] writCommad:cmd];
}

@end
